package logic

import (
	"context"
	"furni-set/furnirpc/furnirpc"
	"furni-set/furnirpc/internal/svc"
	"furni-set/models/mysql"
	"github.com/zeromicro/go-zero/core/logx"
)

type HouseAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseAddLogic {
	return &HouseAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 家具添加
func (l *HouseAddLogic) HouseAdd(in *furnirpc.HouseAddRequest) (*furnirpc.HouseAddResponse, error) {
	// todo: add your logic here and delete this line

	brand := &mysql.Brand{
		Name: in.BrandName,
	}

	if err := brand.FindBrandByName(mysql.DB, in.BrandName); err != nil {
		return nil, err
	}

	class := &mysql.Class{
		Name: in.ClassName,
	}

	if err := class.FindClassByName(mysql.DB, in.ClassName); err != nil {
		return nil, err
	}

	house := &mysql.House{
		Name:    in.Name,
		Price:   float64(in.Price),
		Stock:   in.Stock,
		Image:   in.Image,
		BrandId: int64(brand.ID),
		ClassId: int64(class.ID),
	}

	err := house.HouseAdd(mysql.DB)
	if err != nil {
		return nil, err
	}

	return &furnirpc.HouseAddResponse{
		HouseId: int64(house.ID),
	}, nil
}
